﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JshRCSWebApi.Util.Cache.Redis;

namespace JshRCSWebApi.Util.Cache
{
    public static class RedisFn
    {
        /// <summary>
        /// 获取redis 字符串形式
        /// </summary>
        /// <param name="ls_key"></param>
        /// <returns></returns>
        public static string fn_redis_get_string(string ls_key)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                string ls_value = Redis.Get<string>(ls_key);
                //释放内存
                Redis.Dispose();
                return ls_value;
            }
        }

        /// <summary>
        /// 获取redis 字符串形式(获取整数)
        /// </summary>
        /// <param name="ls_key"></param>
        /// <returns></returns>
        public static int fn_redis_get_int(string ls_key)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                int ls_value = Redis.Get<int>(ls_key);
                //释放内存
                Redis.Dispose();
                return ls_value;
            }
        }

        /// <summary>
        /// 保存redis 字符串形式
        /// </summary>
        /// <param name="ls_key"></param>
        /// <param name="ls_value"></param>
        /// <returns></returns>
        public static bool fn_redis_save_string(string ls_key, string ls_value)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                Redis.Set<string>(ls_key, ls_value);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 保存redis 整数形式
        /// </summary>
        /// <param name="ls_key"></param>
        /// <param name="ls_value"></param>
        /// <returns></returns>
        public static bool fn_redis_save_int(string ls_key, int ls_value)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                Redis.Set<int>(ls_key, ls_value);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 保存redis 字符串形式
        /// </summary>
        /// <param name="ls_key">key</param>
        /// <param name="ls_value">值</param>
        /// <param name="ld_enddate">过期时间</param>
        /// <returns></returns>
        public static bool fn_redis_save_string(string ls_key, string ls_value, DateTime ld_enddate)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                Redis.Set<string>(ls_key, ls_value);
                Redis.ExpireEntryAt(ls_key, ld_enddate);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 设置redis的过期时间
        /// </summary>
        /// <param name="ls_key">redis的key</param>
        /// <param name="ld_datetime">过期时间</param>
        /// <returns></returns>
        public static bool fn_redis_setExpire(string ls_key, DateTime ld_datetime)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                Redis.ExpireEntryAt(ls_key, ld_datetime);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        /// <param name="ls_key"></param>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
      
        public static bool fn_redis_setExpire(string ls_key, TimeSpan timeSpan)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                Redis.ExpireEntryIn(ls_key, timeSpan);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 获取redis 结构体形式
        /// </summary>
        /// <param name="ls_key"></param>
        /// <returns>返回值通过 ser.Deserialize(Redis.Get<byte[]>("uInfo")) as UserInfo;还原成结构体 var ser = new ObjectSerializer();</returns>
        public static byte[] fn_redis_get_Serialize(string ls_key)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                byte[] infos = Redis.Get<byte[]>(ls_key);
                //释放内存
                Redis.Dispose();
                return infos;
            }
        }

        /// <summary>
        /// 保存redis 结构体形式, var ser = new ObjectSerializer();
        /// </summary>
        /// <param name="ls_key">键值</param>
        /// <param name="ls_value">内容通过ser.Serialize()转换为btye[]</param>
        /// <returns></returns>
        public static bool fn_redis_save_Serialize(string ls_key, byte[] ls_value)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                Redis.Set<byte[]>(ls_key, ls_value);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 保存redis 结构体形式, var ser = new ObjectSerializer();
        /// </summary>
        /// <param name="ls_key">键值</param>
        /// <param name="ls_value">内容通过ser.Serialize()转换为btye[]</param>
        /// <param name="ld_enddate">过期时间</param>
        /// <returns></returns>
        public static bool fn_redis_save_Serialize(string ls_key, byte[] ls_value, DateTime ld_enddate)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                Redis.Set<byte[]>(ls_key, ls_value);
                Redis.ExpireEntryAt(ls_key, ld_enddate);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 获取redis 返回List类型
        /// </summary>
        /// <param name="ls_key"></param>
        /// <returns></returns>
        public static List<string> fn_redis_get_List(string ls_key)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                List<string> infos = Redis.GetAllItemsFromList(ls_key);
                //释放内存
                Redis.Dispose();
                return infos;
            }
        }

        /// <summary>
        /// 保存redis List类型
        /// </summary>
        /// <param name="ls_key">键值</param>
        /// <param name="ls_value">List类型</param>
        /// <returns></returns>
        public static bool fn_redis_save_List(string ls_key, List<string> ls_value)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                ls_value.ForEach(x => Redis.AddItemToList(ls_key, x));
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 保存redis List类型
        /// </summary>
        /// <param name="ls_key">键值</param>
        /// <param name="ls_value">List类型</param>
        /// <param name="ld_enddate">过期时间</param>
        /// <returns></returns>
        public static bool fn_redis_save_List(string ls_key, List<string> ls_value, DateTime ld_enddate)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                ls_value.ForEach(x => Redis.AddItemToList(ls_key, x));
                Redis.ExpireEntryAt(ls_key, ld_enddate);
                //释放内存
                Redis.Dispose();
                return true;
            }
        }

        /// <summary>
        /// 获取redis 返回Hash类型
        /// </summary>
        /// <param name="ls_key"></param>
        /// <returns></returns>
        public static byte[] fn_redis_get_Hash(string ls_hashid, string ls_key)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                HashOperator operators = new HashOperator();
                byte[] infos = operators.Get<byte[]>(ls_hashid, ls_key);
                //释放内存
                Redis.Dispose();
                operators.Dispose();
                return infos;
            }
        }

        /// <summary>
        /// 保存redis Hash类型
        /// </summary>
        /// <param name="ls_key">键值</param>
        /// <param name="ls_value">Hash类型</param>
        /// <returns></returns>
        public static bool fn_redis_save_Hash(string ls_hashid, string ls_key, byte[] bytelist)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                HashOperator operators = new HashOperator();
                bool results = operators.Set<byte[]>(ls_hashid, ls_key, bytelist);
                //释放内存
                Redis.Dispose();
                operators.Dispose();
                return results;
            }
        }

        /// <summary>
        /// 保存redis Hash类型
        /// </summary>
        /// <param name="ls_key">键值</param>
        /// <param name="ls_value">Hash类型</param>
        /// <param name="ld_enddate">过期时间</param>
        /// <returns></returns>
        public static bool fn_redis_save_Hash(string ls_hashid, string ls_key, byte[] bytelist, DateTime ld_enddate)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                HashOperator operators = new HashOperator();
                bool results = operators.Set<byte[]>(ls_hashid, ls_key, bytelist);
                Redis.ExpireEntryAt(ls_key, ld_enddate);
                //释放内存
                Redis.Dispose();
                operators.Dispose();
                return results;
            }
        }

        #region 移除key

        /// <summary>
        /// 移除key
        /// </summary>
        /// <param name="ls_key"></param>
        /// <returns></returns>
        public static bool fn_redis_remove_key(string ls_key)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                bool results = Redis.Remove(ls_key);
                //释放内存
                Redis.Dispose();
                return results;
            }
        }

        /// <summary>
        /// 移除hash中的key
        /// </summary>
        /// <param name="ls_hashid"></param>
        /// <param name="ls_key"></param>
        /// <returns></returns>
        public static bool fn_redis_remove_hash_key(string ls_hashid, string ls_key)
        {
            using (IRedisClient Redis = RedisManager.GetClient())
            {
                bool results = Redis.RemoveEntryFromHash(ls_hashid, ls_key);
                //释放内存
                Redis.Dispose();
                return results;
            }
        }

        #endregion
    }
}
